home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / lib / python2.4 / test / test_urllib2net.py < prev    next >
Text File  |  2005-10-18  |  3KB  |  93 lines

  1. #!/usr/bin/env python
  2.  
  3. import unittest
  4. from test import test_support
  5.  
  6. import socket
  7. import urllib2
  8. import sys
  9. import os
  10. import mimetools
  11.  
  12. class URLTimeoutTest(unittest.TestCase):
  13.  
  14.     TIMEOUT = 10.0
  15.  
  16.     def setUp(self):
  17.         socket.setdefaulttimeout(self.TIMEOUT)
  18.  
  19.     def tearDown(self):
  20.         socket.setdefaulttimeout(None)
  21.  
  22.     def testURLread(self):
  23.         f = urllib2.urlopen("http://www.python.org/")
  24.         x = f.read()
  25.  
  26. class urlopenNetworkTests(unittest.TestCase):
  27.     """Tests urllib2.urlopen using the network.
  28.  
  29.     These tests are not exhaustive.  Assuming that testing using files does a
  30.     good job overall of some of the basic interface features.  There are no
  31.     tests exercising the optional 'data' and 'proxies' arguments.  No tests
  32.     for transparent redirection have been written.
  33.  
  34.     setUp is not used for always constructing a connection to
  35.     http://www.python.org/ since there a few tests that don't use that address
  36.     and making a connection is expensive enough to warrant minimizing unneeded
  37.     connections.
  38.  
  39.     """
  40.  
  41.     def test_basic(self):
  42.         # Simple test expected to pass.
  43.         open_url = urllib2.urlopen("http://www.python.org/")
  44.         for attr in ("read", "close", "info", "geturl"):
  45.             self.assert_(hasattr(open_url, attr), "object returned from "
  46.                             "urlopen lacks the %s attribute" % attr)
  47.         try:
  48.             self.assert_(open_url.read(), "calling 'read' failed")
  49.         finally:
  50.             open_url.close()
  51.  
  52.     def test_info(self):
  53.         # Test 'info'.
  54.         open_url = urllib2.urlopen("http://www.python.org/")
  55.         try:
  56.             info_obj = open_url.info()
  57.         finally:
  58.             open_url.close()
  59.             self.assert_(isinstance(info_obj, mimetools.Message),
  60.                          "object returned by 'info' is not an instance of "
  61.                          "mimetools.Message")
  62.             self.assertEqual(info_obj.getsubtype(), "html")
  63.  
  64.     def test_geturl(self):
  65.         # Make sure same URL as opened is returned by geturl.
  66.         URL = "http://www.python.org/"
  67.         open_url = urllib2.urlopen(URL)
  68.         try:
  69.             gotten_url = open_url.geturl()
  70.         finally:
  71.             open_url.close()
  72.         self.assertEqual(gotten_url, URL)
  73.  
  74.     def test_bad_address(self):
  75.         # Make sure proper exception is raised when connecting to a bogus
  76.         # address.
  77.         self.assertRaises(IOError,
  78.                           # SF patch 809915:  In Sep 2003, VeriSign started
  79.                           # highjacking invalid .com and .net addresses to
  80.                           # boost traffic to their own site.  This test
  81.                           # started failing then.  One hopes the .invalid
  82.                           # domain will be spared to serve its defined
  83.                           # purpose.
  84.                           # urllib2.urlopen, "http://www.sadflkjsasadf.com/")
  85.                           urllib2.urlopen, "http://www.python.invalid/")
  86.  
  87. def test_main():
  88.     test_support.requires("network")
  89.     test_support.run_unittest(URLTimeoutTest, urlopenNetworkTests)
  90.  
  91. if __name__ == "__main__":
  92.     test_main()
  93.